package com.dj.shopapp.controller;

import com.dj.shop.bean.CartGoods;
import com.dj.shop.controller.BaseController;
import com.dj.shop.service.ShoppingCartService;
import com.dj.shop.vo.Result;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renshp
 * @Title: ShoppingCartController
 * @Description: TODO
 * @date 2019/3/6
 */
@Controller
@RequestMapping("/app/shoppingcart/v1")
public class ShoppingCartController extends BaseController {

    @Resource(name="shoppingCartService")
    ShoppingCartService shoppingCartService;

    /***
     * 添加商品到购物车
     * @param userId
     * @param shopId
     * @param shopGoodsId
     * @return
     */
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    public @ResponseBody Result addGoods(int userId, int shopId, int shopGoodsId){
        if (shoppingCartService.addGoods(userId, shopId, shopGoodsId) < 1){
            return Result.error();
        }

        return Result.success();
    }

    /***
     * 从购物车删除/减少商品
     * @param userId
     * @param shopId
     * @param shopGoodsId
     * @return
     */
    @RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
    public @ResponseBody Result deleteGoods(int userId, int shopId, int shopGoodsId){
        if (shoppingCartService.deleteGoods(userId, shopId, shopGoodsId) < 1){
            return Result.error();
        }

        return Result.success();
    }

    /***
     * 获取购物车内所有商品
     * @param userId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/queryAllGoods", method = RequestMethod.GET)
    public @ResponseBody Result queryAllGoods(int userId, int shopId){
        List<CartGoods> cartGoodsList = shoppingCartService.queryAllGoods(userId, shopId);

        return Result.success(cartGoodsList);

    }

    /***
     * 清空购物车
     * @param userId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/clearAllGoods", method = RequestMethod.POST)
    public @ResponseBody Result clearAllGoods(int userId, int shopId){
        if (shoppingCartService.clearAllGoods(userId, shopId) < 1){
            return Result.error();
        }

        return Result.success();
    }

}
